package com.qf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.dao.ReservationMapper;
import com.qf.entity.*;
import com.qf.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约列表管理
 */
@Service//服务注解
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;


    /**
     * 通过id查询检查组
     * @param id
     * @return
     */
    @Override
    public ReservationList findById(Integer id) {
        return reservationMapper.findById(id);
    }
    /**
     * 通过检查组id查询检查项id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return reservationMapper.findCheckItemIdsByCheckGroupId(id);
    }
    /**
     * 查询所有套餐
     * @return
     */
    public List<SetMeal> findAll() {
        return reservationMapper.findAll();
    }

    /**
     * 新增检查组
     * @param reservationList
     * @param setmealIds
     */
    @Override
    public void add(ReservationList reservationList, Integer[] setmealIds) {
        reservationMapper.add(reservationList);
        setCheckGroupAndCheckItem(reservationList.getId(),setmealIds);
    }
    /**
     * 设置检查组和检查项关联
     * @param reservationId
     * @param setmealidIds
     */
    public void setCheckGroupAndCheckItem(Integer reservationId,Integer[] setmealidIds){
        if(setmealidIds != null &&setmealidIds.length > 0){
            for (Integer setmealidId : setmealidIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("reservation_id",reservationId);
                map.put("setmealid-id",setmealidId);
                reservationMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
    /**
     * 取消预约
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        reservationMapper.deleteAssociation(id);
        reservationMapper.deleteById(id);
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = reservationMapper.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
